#define epsilon 1e-4
#define mint .001

#define sphere( p,r)  ( length(p) - r )

float box( vec2 p, vec2 b )
{
  vec2 d = abs(p) - b;
  return min(max(d.x,d.y),0.) + length(max(d,0.));
}

float sdf(vec2 pos)
{
    float a1 = box(   pos,vec2( .1,  .2 ) );
    float a2 = sphere(pos-vec2( .1,  0.), .1);
    float a3 = sphere(pos-vec2(-.3, -.3), .1);
    return min(a1, min(a2, a3));
}

float shadow(vec2 light, vec2 pos, float hard)
{
    vec2  d = light - pos;
	float l = length(d),
        res = 1.;
    
    for( float t=mint, h=0.0; t < l; t+= h)
    {
        h = sdf(pos + d/l*t);
        if( h < epsilon ) return 0.;
        res = min( res, hard*h/t );
    }
    return res;
}

void mainImage( out vec4 O, vec2 U )
{
    vec2  R = iResolution.xy,
         uv = (    U      -.5* R ) / R.y,
       mpos = ( iMouse.xy -.5* R ) / R.y;
	
    O = sdf(uv)*vec4(1, 0, 0, 1);
    //O = (sdf(uv)<0.?1.:0.)*vec4(1, 0, 0, 1);
    
    float shad1 = 0.0;
    float shad2 = 0.0;

    //shad1 = shadow(vec2(3.*sin(iTime), -4.), uv, 30.);
    //shad2 = shadow(mpos, uv, 30.);
  
    O += (shad1+shad2)/2.;
}